package main

import (
	"os"
	"os/signal"
	"server_cluster/common/logger"
	"server_cluster/http/controller"
	"server_cluster/http/dao"
	"server_cluster/http/http_config"
	"server_cluster/http/service"
	"syscall"
)

func main() {
	http_config.InitConfig()
	logger.InitLogger("httpServer", http_config.GetConfig().LogLevel)
	logger.Warn("httpserver start")
	defer func() {
		logger.CloseLogger()
	}()

	d, err := dao.NewDao()
	if err != nil {
		panic(err)
	}

	s, err := service.NewService(d)
	if err != nil {
		panic(err)
	}

	_, err = controller.NewController(s)
	if err != nil {
		panic(err)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		select {
		case s := <-c:
			logger.Warn("get a signal %s", s.String())
			switch s {
			case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
				logger.Warn("httpserver exit")
				d.CloseDao()
				return
			case syscall.SIGHUP:
			default:
				return
			}
		}
	}
}
